package com.eco.fanliapp.taobao;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkScTpwdConvertRequest;
import com.taobao.api.request.TbkTpwdConvertRequest;
import com.taobao.api.request.TbkTpwdCreateRequest;
import com.taobao.api.response.TbkScTpwdConvertResponse;
import com.taobao.api.response.TbkTpwdConvertResponse;
import com.taobao.api.response.TbkTpwdCreateResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Service
public class TklController {


    /**
     * 解析淘口令
     *
     * @param tkl
     * @return
     */
    @RequestMapping(value = "/taobao/analyzeTkl", method = RequestMethod.GET)
    public TbkTpwdConvertResponse tklGet(String tkl, Long adzoneId) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkTpwdConvertRequest req = new TbkTpwdConvertRequest();
        req.setPasswordContent(tkl);
        req.setAdzoneId(adzoneId == null ? 108944700127L : adzoneId);
        TbkTpwdConvertResponse response = null;
        try {
            response = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        if (response != null && response.getData() != null) {
            return response;
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        TklController tklController = new TklController();
        String generalizePid = "mm_339150057_530100389_109291550327";
        Long adzoneId = Long.parseLong(generalizePid.split("_")[3]);
        Long siteId = Long.parseLong(generalizePid.split("_")[2]);
        tklController.tklGet2("￥NezLYCdHXWs￥", adzoneId, siteId, "61014207cdce15bb2cc1533b5c5d1ea2ce1c16ad46a53df3322183257");
    }
    /**
     * 淘宝客-服务商-淘口令解析&转链
     * @param tkl
     * @return
     */
    @RequestMapping(value = "/taobao/analyzeTkl2", method = RequestMethod.GET)
    public TbkScTpwdConvertResponse tklGet2(String tkl, Long adzoneId,Long siteId,String sessionKey) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkScTpwdConvertRequest req = new TbkScTpwdConvertRequest();
//        TbkTpwdConvertRequest req = new TbkTpwdConvertRequest();
        req.setPasswordContent(tkl);
//        req.setAdzoneId(109052450405l);
//        req.setSiteId(543150047L);
        req.setAdzoneId(adzoneId == null ? 109052450405l : adzoneId);
        req.setSiteId(siteId == null ? 543150047L : siteId);
        TbkScTpwdConvertResponse response = null;
        try {
            response = client.execute(req, sessionKey == null ? "6102a08bc1be907f1228623078bb1a9efd82937e95f21ca2200668676028" : sessionKey);
//            response = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        if (response != null && response.getData() != null) {
            return response;
        } else {
            return null;
        }
    }
    /**
     * 生成淘口令
     * @param itemTitle
     * @param ItemPic
     * @param couponUrl
     * @return
     */
    @RequestMapping(value = "/taobao/createTkl", method = RequestMethod.POST)
    public Object tklPost(String itemTitle, String ItemPic, String couponUrl) {
        TaobaoClient client = null;
        client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();
        req.setText(itemTitle);
        req.setUrl(couponUrl);
        req.setLogo(ItemPic);
        TbkTpwdCreateResponse rsp = null;
        try {
            rsp = client.execute(req);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        if (rsp.getData().getModel() == null) {
            rsp.getData().setModel("");
        }
        return rsp;
    }
}
